from mongoUtile import mongo
import time
from hssplider.models.ArticleContent import ArticleContent
from hssplider.models.Article import Article


class MongoDBPipeline(object):
    def __init__(self):
        self.db = mongo()

    def process_item(self, item, spider):
        if item.name == "article":
            valid = True
            for data in item:
                if not data:
                    valid = False
            if valid:
                self.insert2update(item)
        elif item.name == "content":
            valid = True
            for data in item:
                if not data:
                    valid = False
            if valid:
                self.insert3update(item)
        else:
            return item

    def insert2update(self, item):
        if item["title"]:
            res = self.db.find_one("note", {"title": item["title"]})
            if res:
                if res['status'] != item["status"]:
                    item["upTime"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                    self.db.update("note", {"title": item["title"]}, dict(item))
            else:
                item["upTime"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                self.db.insert("note", dict(item))

    def insert3update(self, item):
        if item["contenttitle"]:
            resl = self.db.find_one("notecontent", {"contenttitle": item["contenttitle"], "author": item["author"],
                                                    "floor": item["floor"]})
            if not resl:
                self.db.insert("notecontent", dict(item))
